package com.lx.ui;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener, ActionListener {
    int[][] data = new int[4][4];
    int[][] win = {
            {1,2,3,4},
            {5,6,7,8},
            {9,10,11,12},
            {13,14,15,0}
    };
    Random R = new Random();
    int x = 0;
    int y = 0;
    int step = 0;
    String path = "day09\\image\\animal\\animal2\\";
    //初始化横条菜单栏
    JMenuBar jMenuBar = new JMenuBar();

    //        初始化菜单栏中的条目
    JMenu function = new JMenu("功能");
    JMenu aboutus = new JMenu("关于我们");
    JMenu more = new JMenu("更多图片");

    //初始化条目的下拉选项
    JMenuItem replaygame = new JMenuItem("重新游戏");
    JMenuItem relogin = new JMenuItem("重新登陆");
    JMenuItem exit = new JMenuItem("退出");
    JMenuItem girl = new JMenuItem("美女");
    JMenuItem animal = new JMenuItem("动物");
    JMenuItem sport = new JMenuItem("运动");


    JMenuItem collection = new JMenuItem("公众号");
    public GameJFrame(){
        initJFram();
        menu();
        initdata();
        initimage();
        this.setVisible(true);
    }

    private void initdata() {
        int[] temp ={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
        Random r = new Random();
        for(int i=0;i<16;i++)
        {
          int index =  r.nextInt(temp.length);
          int num = temp[i];
          temp[i] = temp[index];
          temp[index] = num;
        }
        for(int i=0;i<16;i++)
        {
            if(temp[i]==0)
            {
                x = i / 4;
                y = i % 4;
            }
                data[i/4][i%4] = temp[i];

        }
    }

    private void initimage() {
        //清理缓存
        this.getContentPane().removeAll();
        JLabel stepcount = new JLabel("步数："+step);
        stepcount.setBounds(50,30,100,20);
        this.getContentPane().add(stepcount);
        if (victory())
        {
            ImageIcon ic = new ImageIcon("C:\\Users\\user\\Desktop\\neusoft\\java_learning02\\day09\\image\\win.png");
            JLabel jLabel = new JLabel(ic);
            jLabel.setBounds(203,283,197,73);
            this.getContentPane().add(jLabel);
        }

        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                int number = data[i][j];
                ImageIcon ic = new ImageIcon(path+number+".jpg");
                JLabel jLabel = new JLabel(ic);
                jLabel.setBounds(105*j+83,105*i+134,105,105);
                jLabel.setBorder(new BevelBorder(1));
                this.getContentPane().add(jLabel);

            }
        }
        //设置边框

        //添加背景图片
        ImageIcon ii  = new ImageIcon("day09\\image\\background.png");
        JLabel background = new JLabel(ii);
        background.setBounds(40,40,508,560);
        this.add(background);


        //刷新图片
        this.getContentPane().repaint();
//        this.add(jLabel);
    }

    private void initJFram() {
        //设置游戏窗口的宽高
        this.setSize(603,680);
        //设置标题
        this.setTitle("拼图小游戏单机版");
        //总是置顶
        this.setAlwaysOnTop(true);
        //设置居中
        this.setLocationRelativeTo(null);
        //设置默认关闭方式
        this.setDefaultCloseOperation(3);
        //取消默认居中
        this.setLayout(null);
        //
        this.addKeyListener(this);
    }

    private void menu() {


        //添加功能
        function.add(more);
        function.add(replaygame);
        function.add(relogin);
        function.add(exit);

        more.add(girl);
        more.add(animal);
        more.add(sport);


        aboutus.add(collection);

        jMenuBar.add(function);
        jMenuBar.add(aboutus);
        //绑定事件
        replaygame.addActionListener(this);
        relogin.addActionListener(this);
        exit.addActionListener(this);
        collection.addActionListener(this);
        girl.addActionListener(this);
        animal.addActionListener(this);
        sport.addActionListener(this);
        //将JMenuBar显示出来
        this.setJMenuBar(jMenuBar);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {

        int code = e.getKeyCode();
        if(code==65)
        {
            this.getContentPane().removeAll();
            ImageIcon ic = new ImageIcon(path+"all.jpg");
            JLabel jLabel = new JLabel(ic);
            jLabel.setBounds(83,134,420,420);
            jLabel.setBorder(new BevelBorder(1));
            this.getContentPane().add(jLabel);
            //添加背景图片
            ImageIcon ii  = new ImageIcon("day09\\image\\background.png");
            JLabel background = new JLabel(ii);
            background.setBounds(40,40,508,560);
            this.add(background);


            //刷新图片
            this.getContentPane().repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(victory())
        {
            return;
        }
        int code = e.getKeyCode();
        System.out.println(code);
        if(code==37)
        {
            if(y==3)
            {
                return;
            }
            data[x][y] = data[x][y+1];
            data[x][y+1] = 0;
            y++;
            step++;
            initimage();
        }
        else if(code==38)
        {
            if(x==3)
            {
                return;
            }
            data[x][y] = data[x+1][y];
            data[x+1][y] = 0;
            x++;
            step++;
            initimage();
        }
        else if(code==39)
        {
            if(y==0)
            {
                return;
            }
            data[x][y] = data[x][y-1];
            data[x][y-1] = 0;
            y--;
            step++;
            initimage();
        }
        else if(code==40)
        {
            if(x==0)
            {
                return;
            }
            data[x][y] = data[x-1][y];
            data[x-1][y] = 0;
            x--;
            step++;
            initimage();
        }
        else if(code==65)
        {
            initimage();
        }
        else if(code==87)
        {
            data = new int[][]{
                    {1,2,3,4},
                    {5,6,7,8},
                    {9,10,11,12},
                    {13,14,15,0}
            };
            initimage();
        }

    }

    public boolean victory(){
        for(int i=0;i<data.length;i++)
        {
            for(int j=0;j<data[i].length;j++)
            {
                if(data[i][j]!=win[i][j])
                {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==exit)
        {
            System.out.println("退出");
            //直接关闭虚拟机
            System.exit(0);
        }
        else if(e.getSource()==relogin)
        {
            System.out.println("重新登陆");
            this.setVisible(false);
            new LoginJFrame();
        }
        else if(e.getSource()==replaygame)
        {
            System.out.println("重新游戏");
            step = 0;
            initdata();
            initimage();
        }
        else if(e.getSource()==collection)
        {
            System.out.println("公众号");

            //创建一个弹框对象
            JDialog jDialog = new JDialog();
            //创建一个管理图片的容器
            JLabel jLabel = new JLabel(new ImageIcon("day09\\image\\about.png"));
            //设置位置和宽高
            jLabel.setBounds(0,0,258,258);
            //把图片加到弹框中
            jDialog.getContentPane().add(jLabel);
            //给弹框设置大小
            jDialog.setSize(344,344);
            //让弹框置顶
            jDialog.setAlwaysOnTop(true);
            //让弹框居中
            jDialog.setLocationRelativeTo(null);
            //让弹框不关闭则无法显示下面的界面
            jDialog.setModal(true);
            //让弹框显示出来
            jDialog.setVisible(true);
        }
        else if(e.getSource()==girl)
        {
//            String path = "day09\\image\\animal\\animal2\\";
            path = path.substring(0,12);
            int i = R.nextInt(13)+1;
            path = path + "girl\\girl"+i+"\\";
            step = 0;
            initdata();
            initimage();
            System.out.println(path);
        }
        else if (e.getSource()==animal)
        {
            path = path.substring(0,12);
            int i = R.nextInt(8)+1;
            path = path + "animal\\animal"+i+"\\";
            System.out.println(path);
            step = 0;
            initdata();
            initimage();
        }
        else if(e.getSource()==sport)
        {
            path = path.substring(0,12);
            int i = R.nextInt(10)+1;
            path = path + "sport\\sport"+i+"\\";
            System.out.println(path);
            step = 0;
            initdata();
            initimage();
        }
    }
}
